package com.easepal802s.project.ui.presenter;


import android.util.Log;

import com.ogawa.base.Constant.BleConstant;
import com.easepal802s.project.ble.BleController;
import com.easepal802s.project.ble.HexUtils;
import com.easepal802s.project.ui.iview.IProMasDetailView;
import com.ogawa.base.utils.CommonUtil;

/**
 * 专业按摩内页
 * Created by FHB on 2018/3/28
 */

public class ProMasDetailPresenter extends MBasePresenter {
    IProMasDetailView mView;
    private byte mByteStart;
    private byte mByteAir;
    private byte mByte3D;
    private int mAirPosition;
    private int m3DPosition;
    private String mScanData ="-1";
    private static int SCAN_STATE;
    private static boolean IS_CAN_ADJUST;
    private byte mByteData ;
    public ProMasDetailPresenter(IProMasDetailView view) {
        mView = view;
    }



    @Override
    public void getDatas(String data) {
        //当前运行的模式

        if (data.startsWith(BleConstant.BAG1_HEAD)) {
            byte[] buffer = HexUtils.hexToByte(data);
            byte byte3 =buffer[3];
            byte byte11 = buffer[11];

            if (mScanData!=HexUtils.getByteInPosition(data, 4, 4)) {
                mScanData = HexUtils.getByteInPosition(data, 4, 4);

                if (mScanData.equals("1")) {
                    BleController.getInst().setIsScanning(true);
                    mView.showScan();
                }

                else {
                    Log.e("SCAN_STATE","ING");
                    BleController.getInst().setIsScanning(false);
                }


            }



            if(mByteStart!=byte3){
                //暂停标志
                mByteStart = byte3;
                if (HexUtils.getByteInPosition(data,3,1).equals("1")) {
                    mView.isStart(true);

                } else {
                    mView.isStart(false);
                }
            }


            if(mByteAir!=byte11){
                //充气强度
                mByteAir = byte11;
                if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,11,5))){
                    mAirPosition=5;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,11,4))){
                    mAirPosition=4;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,11,3))){
                    mAirPosition=3;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,11,2))){
                    mAirPosition=2;
                }
                else  if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,11,1))){
                    mAirPosition=1;
                }
                else {
                    mAirPosition = 0;
                }
                mView.getAir(mAirPosition);
            }

        }
        if (data.startsWith(BleConstant.BAG2_HEAD)) {
            //剩余时间
            byte[] buffer = HexUtils.hexToByte(data);
            byte byteMin =buffer[17];
            byte byteSec =buffer[18];
            byte byte12 =buffer[12];
            mView.reTime(byteMin,byteSec);

            if(mByte3D!=byte12){
                mByte3D = byte12;
                if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,12,5))){
                    m3DPosition=6;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,12,4))){
                    m3DPosition=5;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,12,3))){
                    m3DPosition=4;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,12,2))){
                    m3DPosition=3;
                }
                else  if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,12,1))){
                    m3DPosition=2;
                }
                else  if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,12,0))){
                    m3DPosition=1;
                }
                else {
                    m3DPosition = 0;
                }
                mView.get3D(m3DPosition);
            }


        }

    }


}
